﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructurePlateHoleCalculators.h"
#include "StructureInstanceLevelUtils.h"
#include "IInstance.h"
#include "IStructureInstanceBuildingStoreyData.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

IMPLEMENT_CALCULATOR_CREATOR(gcmp, PlateHoleAssociatedLevelCalculator);

void PlateHoleAssociatedLevelCalculator::ReportInputDataIds(std::vector<RegenDataId> & oInputDatas) const
{
    const IInstance* pElement = GetElement<IInstance>();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pElement, L"计算器的目标对象为空",L"GDMPLab",L"2024-03-30");

    ElementId plateId = pElement->GetHostElementId();
    if (!plateId.IsValid())
    {
        return;
    }

    // 板洞的Host主体依赖数据收集
    {
        const IInstance* pHostPlateInstance = quick_cast<const IInstance>(GetDocument()->GetElement(plateId));
        DBG_WARN_AND_RETURN_VOID_UNLESS(pHostPlateInstance, L"pHostPlateInstance为空",L"GDMPLab",L"2024-03-30");
        const IStructureInstanceBuildingStoreyData* pPlateStoreyData = IStructureInstanceBuildingStoreyData::Get(pHostPlateInstance);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pPlateStoreyData, L"pPlateStoreyData为空",L"GDMPLab",L"2024-03-30");
        RegenDataId plateStoreyDataId = pPlateStoreyData->GetBuildingStoreyIdRdId();
        oInputDatas.push_back(plateStoreyDataId);
    }
}

void PlateHoleAssociatedLevelCalculator::Execute()
{
    const IInstance* pPlateHoleInstance = GetElement<IInstance>();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pPlateHoleInstance, L"计算器的目标对象为空",L"GDMPLab",L"2024-03-30");

    ElementId plateId = pPlateHoleInstance->GetHostElementId();
    DBG_WARN_AND_RETURN_VOID_UNLESS(plateId.IsValid(), L"计算器的目标对象为空",L"GDMPLab",L"2024-03-30");

    const IInstance* pHostPlateInstance = quick_cast<const IInstance>(GetDocument()->GetElement(plateId));
    DBG_WARN_AND_RETURN_VOID_UNLESS(pHostPlateInstance, L"pHostPlateInstance为空",L"GDMPLab",L"2024-03-30");

    const IStructureInstanceBuildingStoreyData* pPlateStoreyData = IStructureInstanceBuildingStoreyData::Get(pHostPlateInstance);
    DBG_WARN_AND_RETURN_VOID_UNLESS(pPlateStoreyData, L"pPlateStoreyData为空",L"GDMPLab",L"2024-03-30");
    ElementId plateLevelId = pPlateStoreyData->GetBuildingStoreyId();

    bool status = StructureInstanceLevelUtils::SetInstanceStoreyLevel(GetDocument(), pPlateHoleInstance->GetElementId(), plateLevelId);
    DBG_WARN_AND_RETURN_VOID_UNLESS(status, L"设置所属楼层失败",L"GDMPLab",L"2024-03-30");
}
